package com.mavenq.fly.utils.excel;

import lombok.Data;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

// 使用示例
class ExcelUtilDemo {

    public static void main(String[] args) throws FileNotFoundException {
        basicExport();
        customWidthExport();
        advancedExport();
        importDemo();
    }

    public static void importDemo() throws FileNotFoundException {
        FileInputStream fis = new FileInputStream("users_custom_width.xlsx");
        String[] headers = {"姓名", "年龄", "邮箱", "创建时间", "备注信息"};
        String[] fieldNames = {"name", "age", "email", "createTime", "remark"};

        List<User> userList = ExcelUtil.importFromExcel(fis, headers, fieldNames, User.class, true);
        userList.forEach(user -> System.out.println(user.getName() + " - " + user.getEmail()));
    }

    public static void basicExport() throws FileNotFoundException {
        List<User> userList = Arrays.asList(
                new User("张三", 25, "zhangsan@example.com", new Date()),
                new User("李四", 30, "lisi@example.com", new Date()),
                new User("这是一个很长的名字测试", 28, "verylongemailaddress@example.com", new Date())
        );

        String[] headers = {"用户姓名", "用户年龄", "电子邮箱地址", "账号创建时间"};
        String[] fieldNames = {"name", "age", "email", "createTime"};

        FileOutputStream fos = new FileOutputStream("users_optimized.xlsx");
        ExcelUtil.exportToExcel(userList, headers, fieldNames, "用户列表", fos, true);
    }

    public static void advancedExport() throws FileNotFoundException {
        List<User> userList = Arrays.asList(
                new User("张三", 25, "zhangsan@example.com", new Date()),
                new User("李四", 30, "lisi@example.com", new Date()),
                new User("这是一个很长的名字测试", 28, "verylongemailaddress@example.com", new Date())
        ); // ... 数据准备

        String[] headers = {"用户姓名", "用户年龄", "电子邮箱地址", "账号创建时间"};
        String[] fieldNames = {"name", "age", "email", "createTime"};

        // 高级配置
        ExcelUtil.ExcelExportConfig config = new ExcelUtil.ExcelExportConfig();
        config.setHeaderBgColor(IndexedColors.DARK_BLUE);
        config.setHeaderFontSize((short) 16);
        config.setHeaderRowHeight((short) 600); // 更高的表头行
        config.setDataRowHeight((short) 450);   // 更高的数据行
        config.setDefaultColumnWidth(20);       // 默认更宽的列
        config.setWrapText(true);               // 启用自动换行
        config.setDataAlignment(HorizontalAlignment.CENTER);

        FileOutputStream fos = new FileOutputStream("users_advanced.xlsx");
        ExcelUtil.exportToExcelAdvanced(userList, headers, fieldNames, "用户列表", fos, true, config);
    }

    public static void customWidthExport() throws FileNotFoundException {
        List<User> userList = Arrays.asList(
                new User("张三", 25, "zhangsan@example.com", new Date()),
                new User("李四", 30, "lisi@example.com", new Date()),
                new User("这是一个很长的名字测试", 28, "verylongemailaddress@example.com", new Date())
        ); // ... 数据准备

        String[] headers = {"姓名", "年龄", "邮箱", "创建时间", "备注信息"};
        String[] fieldNames = {"name", "age", "email", "createTime", "remark"};

        ExcelUtil.ExcelExportConfig config = new ExcelUtil.ExcelExportConfig();
        // 针对不同列设置不同的最小最大宽度
        config.setMinColumnWidth(10);
        config.setMaxColumnWidth(30);
        config.setDefaultColumnWidth(15);

        FileOutputStream fos = new FileOutputStream("users_custom_width.xlsx");
        ExcelUtil.exportToExcelAdvanced(userList, headers, fieldNames, "用户列表", fos, true, config);
    }

    @Data
    public static class User {
        private String name;
        private Integer age;
        private String email;
        private Date createTime;
        private String remark;

        public User() {
        }

        ;

        // 构造方法、getter、setter
        public User(String name, Integer age, String email, Date createTime) {
            this.name = name;
            this.age = age;
            this.email = email;
            this.createTime = createTime;
            this.remark = "这是一个备注信息，可能会比较长，用于测试自动换行功能";
        }

    }
}